<?php
/**
 * Created by Pako
 * Date: 2018/8/19
 * Time: 18:13
 */

namespace App\Http\Request\Auth;


use App\Http\Request\Store;
use App\Models\Auth\Permission;

class PermissionStore extends Store
{
    public function rules()
    {
        $method = strtoupper($this->method());
        if ($method == 'PUT') {
            return [
                'permission_id' => 'required|integer',
                'name' => 'required',
                'pid' => 'required|integer'
            ];
        }
        return [
            'name' => 'required|unique:auth_permission,name',
            'pid' => 'required'
        ];
    }

    public function attributes()
    {
        return [
            'permission_id' => '权限ID',
            'name' => '权限名称',
            'pid' => '父ID'
        ];
    }


    /**
     *
     * after
     * @author Paco
     * @createTime 2018/8/21 18:30
     * @return bool|mixed
     */
    public function after()
    {
        $method = strtoupper($this->method());
        if ($method == 'PUT') {
            $params = $this->validateData();
            $permission = Permission::query()->where('name', $params['name'])->first();

            if ($permission && $permission->permission_id != $params['permission_id']) {
                return $this->throwRespondError(10400);
            }
        }
        return true;
    }


}